Jenkins Kata 5: Docker Container Build Step

Learn about adding a Dockerfile, installing a Docker plugin, running a private Docker repository, and adding a Docker build step.

Step 1: Add a Dockerfile#

To do this:

  • Use the NGINX image and copy the files to the container.
  • Enter the following into the text editor:
Dockerfile
  • Save the changes.
Creating a Dockerfile

Build the Dockerfile as follows:

Building the Dockerfile

Command's Parameters

Command / Parameter

Description

docker build

This is the parent command. Docker build creates a new image from a Dockerfile.

-t

This adds a tag to the image.

storelist_image

This is the tag to be added to the new image.

.

This is a reference to the local directory, which indicates to the docker command where to find the Dockerfile.

Cody’s first step in containerizing the “web-storelist” application is to add a Dockerfile. The Dockerfile contains directives that copy the files of the application to an image based on the official NGINX image. NGINX is an open-source HTTP server. Cody then tests the Dockerfile by building it manually with the docker build command.

Run the image locally as follows:

Running the container image

Command's Parameters

Command / Parameter

Description

container

This is the parent command. The container parent command executes commands related to containers.

run

The run command runs a container.

-p

This maps ports between the container and the host.


80:80

This is the value used by the -p parameter to map ports. This value maps port 80 on the host to port 80 in the container. Requests to port 80 on the host IP address will be routed to port 80 in the container.

storelist_image

This is the name of the image to run.

Curl content of localhost

Commands

Command / Parameter

Description

curl

The curl command sends an HTTP request to a URL and returns the response to the terminal.

localhost

This is an alias for the local computer.

The commands to add the Dockerfile to Cody’s repository index, commit, and push are given below:

Adding and commiting the Dockerfile

Command's Parameters

Command / Parameter

Description

git commit

This creates a new commit in the local repository.

-a

This includes all modified files in the new commit.

-m

This sets the commit message in the command line.

"Adding Dockerfile"

This is the commit message that will be associated with the new commit.

git push

This pushes commits to a remote Git server. All local commits missing from the remote will be pushed.

origin

This is the registered name of the remote Git server. The default name is `origin`.

master

This is the name of the branch to push.

These commands commit the addition of the Dockerfile to the local “web-storelist” Git repository, then push that commit to the Gogs server. If we return to Jenkins, we should see a new, recent build in the “web-storelist” job. When the commit was pushed to Gogs, the webhook executed and triggered the build. We can also view the workspace of the job and should see the Dockerfile in the list of files. The job pulled the “web-storelist” repository when it was executed, updating the workspace with the latest commits.

Step 2: Install the Docker plugin#

The following commands to install the Docker plugin:

  • Click Jenkins at the top left.
  • Click “Manage Jenkins.”
  • Click “Manage Plugins.”
Click "Manage Plugins"
Click "Manage Plugins"
  • Click the “Available plugins” tab.
  • Enter “docker build” in the filter field.
  • Check the box next to “Cloudbees Docker Build and Publish” plugin.
  • Click “Install without restart.”
  • Wait for install to complete.
  • Click “Go back to the top page.”
Select the “CloudBees Docker Build and Publish” plugin
Select the “CloudBees Docker Build and Publish” plugin

This step installs another Jenkins plugin, the Cloudbees Docker Build and Publish plugin. Jenkins is an open-source system. Cloudbees is a company that offers Jenkins with enterprise services. This plugin is provided as free open source by Cloudbees.

The Cloudbees Docker plugin supports building and uploading images. These things could be done with a shell script, but the plugin simplifies the job configuration. The complexities of image build and publication are handled by it. As we’ll see in a later step, only four configuration values are required to build and publish a new image.

Step 3: Run a private Docker registry#

Return to the terminal window and run the following command:

Command's Parameters

Command / Parameter

Description

container

This is the parent command. The container parent command executes commands related to containers.

run

The run command runs a container.

-p

This maps ports between the container and the host.


5000:5000

This is the value used by the -p parameter to map ports. This value maps port 5000 on the host to port 5000 in the container. Requests to port 5000 on the host IP address will be routed to port 5000 in the container.

--name

This assigns a name to the locally running container (if omitted, a random name is assigned).

registry

This is the name to assign to the container.


registry:2

This is the image name to run. The name of the container is `registry` while `2` is the tag. Tags are metadata that can be added to images to specify details such as version or date built. This image is stored on Docker Hub, provided by Docker as a private image repository.

Docker images can be uploaded to a public repository, such as Docker Hub. Public repositories are typically used to share public, open-source images. The “web-storelist” application isn’t exactly private, but it also isn’t very useful to anyone but the Coder family. Cody has, therefore, decided to publish the “web-storelist” image to a private repository instead of the public Docker Hub.

Docker Hub provides a private repository image called registry. Cody runs the registry container locally, so the Jenkins job can upload images to it as they’re built. When the images are deployed as running containers, they will be pulled from the private repository.

Step 4: Add a Docker build#

The following are the steps to add a Docker build:

  • Click the “Jenkins” icon at the top left.
  • Select the “web-storelist” job.
  • Click “Configure.”
Click “Configure”
Click “Configure”

  • Select the “Build Steps” tab.
  • Click “Add build step.”
  • Select “Docker Build and Publish.”
Adding a Docker build step
Adding a Docker build step
  • Repository Name: “dk/storelist.”
  • Tag: “${BUILD_NUMBER}”
  • Docker host URI: “unix:///var/run/docker.sock”
  • Docker registry URL: “https://ed-6091404232622080.educative.run:3000/
  • Click the “Advanced” option and then select the “Skip Push” option.
  • Click “Save.”
Entering all the required fields for the Docker build
Entering all the required fields for the Docker build
Click on “Advance” and select “Skip Push”
Click on “Advance” and select “Skip Push”

Commands

Command / Parameter

Description


dk/storelist

This is the name to assign to the image that's built by this step. Image names can be separated by a slash in a host/name format. Organizations can use this format to publish multiple images under a single “host” name.


${BUILD_NUMBER}

This is the tag assigned to the image. Recall from Kata 3 that ${BUILD_NUMBER} is an environment variable. Its value is the number of the running Jenkins job. This assigns a version number to the image that corresponds to the Jenkins build number.



unix:///var/run/docker.sock

Jenkins needs to know where to send commands to interact with a Docker Engine service. This is a special URI that points to the local Docker Engine. Commands sent to that file are redirected to the local Docker Engine, allowing Jenkins to execute Docker commands on the local system.


https://ed-6091404232622080.educative.run:3000/

This is the URL to the private repository that was started in the previous step. Each time this job executes, it will create a new build and upload the resulting image to the registry private image repository.

Installing the Cloudbees Docker plugin added a new option to the “Add build step” selection list: Docker Build and Publish. This plugin-provided step requires four parameters to build and publish new images to a repository.

The CI pipeline we’re building has taken another step forward: It now includes a build process. Executing the “web-storelist” Jenkins job pulls the latest code from the repository. The build process then creates a new “dk/storelist” Docker image each time the job executes and uploads it to the private repository, where it’s ready to be deployed as a container.

To run the “web-storelist” job, do the following:

  • Click “Build Now.”
Successfully build
Successfully build

This step executes the “web-storelist” job with the Docker Build and Publish step. Note that the build history entry format has changed. It’s no longer just a number. The new build history entry format includes all the tags that were assigned to the new image that was built.

A single Docker image can be assigned multiple tags. The default configuration of the Docker Build plugin assigns two tags to each image on every build. The first is the tag that’s defined in the configuration (this is set to the ${DOCKER_BUILD} environment variable.) The second is latest.

If a tag is not specified when the docker container run command is executed, Docker will look for an image with a latest tag and run that one. The Docker Build plugin assigns a latest tag to each new image, which moves the tag from the previous image built to the newest one. This ensures that each run of the “dk/storelist” image will run the most recently built one by default. It’s still possible, if needed, to run a previously built container by specifying a prior tag in a docker container run command.

Run the following command:

Copying the image name
Copying the image name

The command above will display all the running images. We have to copy the image name of “/dk/storelist” and run the container of that image using the following command:

Running the container of the copied image
Running the container of the copied image

Command's Parameters

Command / Parameter

Description

docker container

This is the Docker parent command.

run

This runs a container.

-d

This runs a container in disconnected mode.

-p

This maps ports from the host to the container.

82:80

This maps port 80 on the host to port 80 in the container.



ed-6091404232622080.educative•run/dk/storelist

This is the URL to the image. Those who've already practiced the Docker katas will notice that this is different from running a Docker Hub image. The full URL indicates to Docker where to find the repository hosting the image to be run. This URL points to the private repository run earlier in this kata.

Jenkins Kata 4: Integrate Git with Jenkins

Jenkins Kata 6: Deploying Containers (Steps 1–3)